<template>
  <div>
    <!-- 内容主体区域 -->
    <dtDialog :title="textMap[dialogStatus]" :visible.sync="visible" :is-loading="loading" @closeDialog="handleClose()" @comfirmBtn="onSubmit('form')">
      <el-form ref="form" slot="content" :rules="rules" :model="formData" label-width="82px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model.trim="formData.roleName" maxlength="30" autocomplete="off" placeholder="请输入角色名称" show-word-limit class="limit" />
        </el-form-item>
        <el-form-item label="角色编码" prop="roleCode">
          <el-input v-model.trim="formData.roleCode" :disabled="dialogStatus == 'update'" maxlength="30" autocomplete="off" placeholder="请输入角色编码" show-word-limit class="limit" />
        </el-form-item>
        <el-form-item label="排序" prop="roleSort">
          <el-input v-model.trim="formData.roleSort" :oninput="initPositiveInteger()" placeholder="请输入排序" maxlength="8" show-word-limit class="limit" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="formData.remark" maxlength="100" show-word-limit type="textarea" autocomplete="off" placeholder="请输入备注" class="limit" />
        </el-form-item>
      </el-form>
    </dtDialog>
  </div>
</template>

<script>
import roleApi from '@/framework/api/permissionCenter/roleManage'
import { positiveInteger } from '@/framework/utils/validate'
import { uuId } from '@/framework/utils/uuid'
export default {
  name: 'RoleAdd',
  data() {
    return {
      visible: false,
      dialogStatus: '',
      textMap: {
        update: '修改角色',
        create: '新增角色'
      },
      loading: false,
      formData: {
        roleId: '',
        roleName: '',
        roleCode: '',
        roleSort: '',
        remark: ''
      },
      subDisabled: false,
      roleTypeList: [],
      rules: {
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        roleCode: [{ required: true, message: '请输入角色编码', trigger: 'blur' }
        ],
        roleSort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 校验排序规则
    initPositiveInteger(data) {
      return positiveInteger(data)
    },
    // 关闭对话框触发
    handleClose() {
      // 清空数据关闭页面
      this.$nextTick(() => {
        this.formData = {}
        this.$refs['form'].resetFields()
      })
    },
    async open(row, dialogStatus) {
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
        this.visible = true
        this.formData.roleCode = uuId(8)
      } else {
        if (row.roleCode === 'superAdmin') {
          this.$dtMessage({
            type: 'error',
            message: '无法修改超级管理员'
          })
          return
        } else {
          this.visible = true
          const res = await roleApi.getRoleById(row.roleId)
          this.formData = res.data
        }
      }
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.submit()
        } else {
          return false
        }
      })
    },
    async submit() {
      let res = null
      this.subDisabled = true
      try {
        if (this.formData.roleId) {
          this.formData.dataScopeType = null
          res = await roleApi.updateRole(this.formData)
        } else {
          res = await roleApi.addRole(this.formData)
        }
        if (res.code === '00000') {
          this.$message({
            type: 'success',
            message: this.dialogStatus === 'create' ? '新增成功' : '修改成功'
          })
          this.$parent.getList()
          this.visible = false
        }
        this.loading = false
        this.subDisabled = false
      } catch (error) {
        this.loading = false
        this.subDisabled = false
      }
    }
  }
}
</script>

<style scoped>

</style>
